package com.cn.dim.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Chkpoint;
import com.cn.dim.pojo.Pages;
import com.cn.dim.service.ChkpointService;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/chkpoint")
public class ChkpointController {

	@Resource
	private ChkpointService chkpointService;
	@RequestMapping(value ="/queryChkpoint",method = RequestMethod.GET)
	@ResponseBody
	//Pages是自己定义的页面实体�?
	public Map<String,Object> queryChkpoint(HttpServletRequest request,Chkpoint c,Pages pages){
		Map<String,Object> param=new HashMap<>();//定义查询参数
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		//参数p是列表页中以查询条件数据临时组成的过渡chkpoint对象
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());
		param.put("chkpoint", c);
		
		try {
			Page<Map<String,Object>> rList = (Page)chkpointService.queryChkpoint(param);
			//Page对象认为是一个以键值对保存的一行记录的多条记录的集合，
			System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/insertChkpoint",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertChkpoint(Chkpoint c)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		String chkpointid=com.cn.dim.utils.StringUtil.makeUUID();
		c.setId(chkpointid);
		c.setIsdel("0");
		
		//这里补充创建人的id的代码
		
		try 
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//创建日期
			c.setCreatetime(cdate);
			
			int flag = chkpointService.insertChkpoint(c);
			if(flag>0)
			{
				reMap.put("message", "插入成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	//用于添加修改页重点选择部门后的联动负责人下拉
	@RequestMapping(value ="/queryUser",method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String,Object>> queryUser(HttpServletRequest request,String depid){
		Map<String,Object> param=new HashMap<>();//查询参数是部门depid；
		param.put("depid", depid);
		List<Map<String,Object>> list = new ArrayList<>();
		try {
			list =chkpointService.queryUser(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}

	
	@RequestMapping(value ="/updateChkpoint",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateChkpoint(Chkpoint c,String chkpointid)  
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		c.setId(chkpointid);//实体对象传来的并不含有id的值，这里要使用页面传过来chkpointid值进行填充
		
		//这里补充修改（删除）人的id的代码
		
		try 
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//创建日期
			c.setUpdatetime(cdate);
			
			int flag = chkpointService.updateChkpoint(c);
			if(flag>0)
			{
				reMap.put("message", "修改成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	//根据参数中的userid取对应的部门，用于修改页面回显
	@RequestMapping(value ="/getDep",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getDep(HttpServletRequest request,String chkpointid){
		Map<String,Object> param=new HashMap<>();//查询参数是用户userid；
		Map<String,Object> reMap=new HashMap<>();
		param.put("chkpointid", chkpointid);
		try {
			List<Map<String,Object>> list =chkpointService.getDep(param);
			reMap.put("data", list);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}

	@RequestMapping(value ="/delChkpoint",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delChkpoint(HttpServletRequest request,String chkpointid) 
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
        Map<String,Object> m=new HashMap<>();//构造service接口中delChkpoint方法的参数
        m.put("chkpointid", chkpointid);
		
		//这里补充修改（删除）人的id的代码
		
		try 
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//创建日期
			m.put("updatetime", cdate);

			int flag = chkpointService.delChkpoint(m);
			if(flag>0)
			{
				reMap.put("message", "删除成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/queryChkpointById",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryChkpointById(HttpServletRequest request,String chkpointid){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		m.put("chkpointid", chkpointid);
		try {
			Chkpoint chkpoint =chkpointService.queryChkpointById(m);
			reMap.put("data", chkpoint);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
}
